import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/api/odoo/sale_inquiry_product_odoo_api.dart';
import 'package:flutter_admin/models/odoo/sale_inquiry_product.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input_num.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import '../../../generated/l10n.dart';

class SaleOfferPrice extends StatefulWidget {
  SaleOfferPrice({this.saleInquiryProduct, required this.isAdd});

  final SaleInquiryProduct? saleInquiryProduct;
  final bool isAdd;

  @override
  _SaleOfferPriceState createState() => _SaleOfferPriceState();
}

class _SaleOfferPriceState extends State<SaleOfferPrice> {
  late SaleInquiryProduct saleInquiryProduct;
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  @override
  void initState() {
    super.initState();
    this.saleInquiryProduct = widget.saleInquiryProduct ?? SaleInquiryProduct();
    if (widget.isAdd == true) {
      // _getPrice();
    }
    setState(() {});
  }

  // _getPrice() async {
  //   ResponseBodyApi responseBodyApi = await SaleInquiryProductOdooApi.getPrice(
  //     data: {
  //       'domainList': [
  //         [
  //           'price_id.product_id.default_code',
  //           '=',
  //           saleInquiryProduct.productCode
  //         ]
  //       ]
  //     },
  //   );
  //   if (responseBodyApi.success!) {
  //     saleInquiryProduct.unitPrice = responseBodyApi.data[0]['price'];
  //     setState(() {});
  //   }
  // }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CryInputNum(
            label: '单价',
            enable: !widget.isAdd,
            value: saleInquiryProduct.unitPrice,
            onSaved: (v) {
              saleInquiryProduct.unitPrice = v as double?;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            saleInquiryProduct.edit = '1';
            SaleInquiryProductOdooApi.savePrice(
                    saleInquiryProduct.toOdooPrice())
                .then((ResponseBodyApi res) {
              if (!res.success!) {
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
            // Navigator.pop(context, saleInquiryProduct);
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(widget.isAdd ? S.of(context).add : S.of(context).edit),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
